/**
 * 
 */
package com.yunyike.controller.shopper;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.fabric.Response;
import com.yunyike.constants.CommonConstant;
import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.exception.BadRequestException;
import com.yunyike.model.Merchandise;
import com.yunyike.model.Shop;
import com.yunyike.model.Shopper;
import com.yunyike.model.User;
import com.yunyike.redis.JedisManager;
import com.yunyike.service.IMerchandiseService;
import com.yunyike.service.IShopService;
import com.yunyike.service.IShopperService;
import com.yunyike.utils.JsonUtils;
import com.yunyike.wx.model.UserInfo;
import com.yunyike.wx.service.IWxUserService;

/**
 * @author zhengqiang.shi
 * 2015年12月13日 下午4:57:20
 */
@Controller
@RequestMapping(value=RequestConstant.SHOPPER)
public class ShopperController extends BaseController{

	private static Logger log = LoggerFactory.getLogger(ShopperController.class);
	
	@Autowired
	private IShopperService shopperService;
	
	@Autowired
	private IWxUserService wxUserService;
	/**
	 * 根据店铺id 获取导购列表
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.SHOPPER_FIND_BY_SHOP+"/{shopId}")
	public String findShopperByShop(@PathVariable String shopId){
		
		// 请求参数校验
		if(StringUtils.isBlank(shopId)){
			throw new BadRequestException("request param not correct.");
		}
		log.info("----->> find shopper by shop,shopId:"+shopId);
		
		// call service
		List<Shopper> shoppers = shopperService.findShopperByShop(shopId);
		
		if(shoppers == null || shoppers.isEmpty()){
			log.info("shopper controller ,----->> shoppers is null.shopId+"+shopId);
			return null;
		}
		
		// format to json
		String shoppersJson = JsonUtils.ObjectAsString(shoppers);
		
		log.info("----->> find shopper by shop end.shoppersJson:"+shoppersJson);
		
		return shoppersJson;
	}
	
	/**
	 * 顾客扫码绑定导购
	 * 在购物车扫一扫进行导购扫码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.SHOPPER_CONNECT+"/{shopperId}")
	public String connectShopper(@PathVariable String shopperId,
			HttpServletRequest request,HttpServletResponse response,Model model){
		try{
			log.info("======connectShopper...shopperId:"+shopperId);
			Shopper shopper = shopperService.findShopperById(shopperId);
			if(null!=shopper){
				// 当前用户
				User user = getCurrentUser();
				if(null==user){
					log.info("controller connectShopper ..... no user");
					throw new BadRequestException();
				}
				UserInfo userInfo = wxUserService.getUserInfo(user.getUserOpenId());
				user.setUserName(userInfo.getNickname());
				JedisManager.setObject(shopperId+CommonConstant.SHOPPER_CONNECT_USER, CommonConstant.USER_KEY_ET, user);
				JedisManager.setObject(user.getUserId()+CommonConstant.SHOPPER_CONNECT_USER, CommonConstant.USER_KEY_ET, shopper);
				log.info("========JedisManager--userId:"+user.getUserId()+",shopperId:"+shopper.getShopperId());
				return "success+"+shopper.getShopId();
			}else{
				log.info("controller connectShopper ..... no shopper");
				return "no this shopper";
			}
			
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		
	}
	
	
}
